package cn.tedu.luban.worker.main.controller;

import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import cn.tedu.luban.worker.main.converter.WorkerAuditConverter;
import cn.tedu.luban.worker.main.converter.WorkerConverter;
import cn.tedu.luban.worker.main.service.WorkerService;
import cn.tedu.luban.worker.protocol.VO.AdminAuditDetailVO;
import cn.tedu.luban.worker.protocol.VO.AdminWorkerListVO;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerDetailVO;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.bo.FrontWorkerBO;
import cn.tedu.luban.worker.protocol.param.AuditSubmitParam;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import com.tedu.inn.protocol.pager.PagerResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    /**
     * 师傅入驻
     * @param param 注册信息
     * @return workerId
     */
    @PostMapping("/worker/create")
    public Result<Long> workerCreate(
            @RequestBody WorkerRegistParam param
//            @RequestHeader("Authorization") String userJson,
//            HttpServletRequest request
    ) throws BusinessException {
//        log.info("拿到头:{}",userJson);
//        String userJson1 = request.getHeader("Authorization");
//        log.info("拿到头:{}",userJson1);
//        UserPrinciple loginToken = LubanSecurityContext.getLoginToken();
//        log.info("拿到头:{}",loginToken);

        // 调用业务层方法
        Long workerId = workerService.workerCreate(param);
        // 包装Result对象返回
        return new Result<>(workerId);
    }


    @Autowired
    private WorkerConverter workerConverter;
    @GetMapping("/worker/detail")
    public  Result<FrontWorkerDetailVO> detailWorker() throws BusinessException {
        // 调用业务逻辑查询师傅数据(BO对象)
        FrontWorkerBO frontWorkerBO = workerService.detailWorker();
        // 转化器转化BO为VO
        FrontWorkerDetailVO frontWorkerDetailVO = workerConverter.bo2vo(frontWorkerBO);

        return new Result<>(frontWorkerDetailVO);
    }

    /**
     * 审核师傅列表
     */
    @PostMapping("/admin/worker/aduit")
    public Result<PagerResult<AdminWorkerListVO>> workerAuditList(
            @RequestBody WorkerListQuery query){

        // WorkerListQuery:  pageNo - 页数 \ pageSize - 每页行数
        // ListRecordTotalBO:  list - 数据 \ total - 总行数
        // PagerResult: totalPage -总页数
        ListRecordTotalBO<AdminWorkerBO> boListRecordTotalBO
                = workerService.workerAuditList(query);

        PagerResult<AdminWorkerListVO> pagerResult
                = workerConverter.bo2PagerResultVO(boListRecordTotalBO, query);

        return new Result<>(pagerResult);
    }

    @Autowired
    private WorkerAuditConverter workerAuditConverter;
    /**
     * 审核师傅详情页

     */
    @PostMapping("/admin/worker/audit/detail")
    public Result<AdminAuditDetailVO> auditDetail(
            @RequestParam("workerId") Long userId) throws BusinessException {
        // 查询师傅详情
        AdminWorkerBO bo = workerService.auditDetail(userId);

        AdminAuditDetailVO adminAuditDetailVO = workerAuditConverter.bo2vo(bo);
        return new Result<>(adminAuditDetailVO);
    }


    /**
     * 师傅后台审核提交
     */
    @PostMapping("/admin/worker/audit/save")
    public Result<Long> auditSubmit(@RequestBody AuditSubmitParam param) throws BusinessException {
        Long auditId = workerService.auditSubmit(param);
        return new Result<>(auditId);
    }

}




